import matplotlib.pyplot as plt
import pandas as pd

# 创建映射字典
gender_dict = {'男': 'Male', '女': 'Female'}
post_dict = {'民警': 'Police', '事业': 'Career', '辅警': 'Auxiliary', '自招人员': 'Self-recruit'}
unit_dict = {
    "珠海路派出所": "Zhuhai Road Police Station",
    "特巡警大队": "Special Patrol Police Team",
    "治安大队": "Public Security Team",
    "指挥中心": "Command Center",
    "界石派出所": "Jieshi Police Station",
    "南海综指": "Nanhai Comprehensive Direction",
    "天福派出所": "Tianfu Police Station",
    "环山派出所": "Huan Shan Police Station",
    "张家产派出所": "Zhangjiachan Police Station",
    "米山派出所": "Mishan Police Station",
    "文登营派出所": "Wendengying Police Station",
    "泽头派出所": "Zetou Police Station",
    "龙山派出所": "Longshan Police Station",
    "五垒岛派出所": "Wulei Island Police Station",
    "小观派出所": "Xiaoguan Police Station",
    "纪委": "Disciplinary Committee",
    "经侦大队": "Economic Investigation Team",
    "警务保障室分局餐厅乐康": "Police Support Department Sub-Bureau Cafeteria Lekang",
    "海岸大队": "Coastal Team",
    "食药环侦大队": "Food and Drug Environmental Investigation Team",
    "南海综指餐厅乐康": "Nanhai Comprehensive Direction Cafeteria Lekang",
    "埠口派出所": "Bukou Police Station",
    "高村派出所": "Gao Village Police Station",
    "宋村派出所": "Song Village Police Station",
    "警务保障室": "Police Support Department",
    "刑侦大队": "Criminal Investigation Team",
    "葛家派出所": "Gejia Police Station",
    "大水泊派出所": "Dashuibo Police Station",
    "网安大队": "Cybersecurity Team",
    "侯家派出所": "Houjia Police Station",
    "警务保障室分局物业": "Police Support Department Sub-Bureau Property",
    "户口管理科": "Household Management Department",
    "宣传": "Publicity",
    "南海新区派出所": "Nanhai New District Police Station",
    "局党委": "Bureau Party Committee",
    "政工": "Political Work",
    "政保大队": "Political Security Team",
    "出入境管理大队": "Entry-Exit Management Team",
    "禁毒大队": "Drug Prohibition Team",
    "法制大队": "Legal System Team",
    "控申部门": "Control Application Department",
    "通信科": "Communication Department",
    "森警大队": "Forest Police Team",
    "特巡警餐厅乐康": "Special Patrol Police Cafeteria Lekang",
    "警务保障室特巡警物业": "Police Support Department Special Patrol Property",
    "特巡警大队餐厅乐康": "Special Patrol Police Team Cafeteria Lekang"
}

df = pd.read_csv('G:/new_data.csv', header = 0)

# 使用字典将每个列的值映射到新的英文列
df['Gender_english'] = df['Gender'].map(gender_dict)
df['Post_english'] = df['Post'].map(post_dict)
df['Unit_english'] = df['Unit'].map(unit_dict)

# 从出生日期中提取出年份
df['Birthdate'] = pd.to_datetime(df['Birthdate'], format='%Y%m%d', errors='coerce').dt.year

fig, axs = plt.subplots(2, 2, figsize=(15,10))  # 创建 2x2 的 subplot 布局

# 柱状图可视化性别、岗位和单位
for i, column in enumerate(['Gender_english', 'Post_english', 'Unit_english']):
    value_counts = df[column].value_counts()
    axs[i//2, i%2].bar(value_counts.index, value_counts.values)
    axs[i//2, i%2].set_xlabel(column)
    axs[i//2, i%2].set_ylabel('Count')
    axs[i//2, i%2].set_title('Distribution of ' + column)
    for tick in axs[i//2, i%2].get_xticklabels():
        tick.set_rotation(45)

# 直方图可视化出生年份
axs[1, 1].hist(df['Birthdate'], bins=20, edgecolor='black')
axs[1, 1].set_xlabel('Year of Birth')
axs[1, 1].set_ylabel('Count')
axs[1, 1].set_title('Distribution of Year of Birth')

plt.tight_layout()
plt.show()